<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入mapbox的依赖 -->
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        const gaode = {
            "version": 8,
            "sources": {
                "raster-tiles": {
                    "type": "raster",
                    "tiles": ["http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}"],
                    "tileSize": 256
                }
            },
            "layers": [{
                "id": "simple-tiles",
                "type": "raster",
                "source": "raster-tiles",
                "minzoom": 0,
                "maxzoom": 22
            }]
        }
        let style = 'mapbox://styles/mapbox/streets-v12'
        /* token */
        mapboxgl.accessToken = 'pk.eyJ1IjoiZXJpYzA5MTciLCJhIjoiY2x3cTI2Zzg0MWY2dDJqcWVpdDI1MHNpcCJ9.k9gLW7ZgLyISNdYHIEEGdQ'
        const map = new mapboxgl.Map({
            /* target-ol */
            container: 'map', // container ID
            /* style-layers */
            style: gaode, // style URL
            center: [114.30, 30.50], // starting position [lng, lat]
            zoom: 12, // starting zoom
        });
        const data = {
            type: "Feature",
            geometry: {
                type: "Point",
                coordinates: [114.30, 30.50]
            }
        }
        /* 加载数据 */

        map.on("style.load", () => {
            /* map.addLayer */
            map.addLayer({
                id: "p-layer",
                source: {
                    type: "geojson",
                    /* source- Features */
                    data
                },
                type: "circle",
                paint: {
                    'circle-radius': 30
                }
            })
        })
        map.on("click", () => {
            /* 1、加载的图层最终会放到style中  layers */
            let layers = map.getStyle().layers
            /*2、 removeLayer(id) */
            let id = layers[0].id;
            console.log(id)
            map.removeLayer(id);
        })
    </script>
</body>

</html>